import json

from django.shortcuts import render

# Create your views here.
from stw_app import models
from stw_app.models import UserInfo
from django.shortcuts import render, redirect
from django.http import JsonResponse, HttpResponse
from django.views.decorators.csrf import csrf_exempt

from stw_app.decorator import already_login
import requests

@already_login
@csrf_exempt
def user_infos(request):
    if request.method == "POST":
        data_list=[]
        objects=UserInfo.objects
        for object in objects.order_by('id'):
            data={
                'id':object.id,
                'age':object.age,
                'id_card':object.id_card,
                'phone':object.phone,
                'name':object.name,
                'gender':object.gender,
                'signature':object.signature,
                'nick_name':object.nick_name,
                'area':object.area,
                'avatar':object.avatar,
                'type':object.type,
                'career':object.career,
                'lowest_wage':object.lowest_wage,
                'highest_wage':object.highest_wage,
                'profile':object.profile,
            }
            data_list.append(data)
        return JsonResponse({'success': True, 'data': data_list})
    else:
        return JsonResponse({'success': False})

@already_login
@csrf_exempt
def updateuser(request):
    if request.method == "POST":
        data_list = json.loads(request.body.decode('utf-8'))
        id=data_list[0].get('id')
        name = data_list[0].get('name')
        nick_name = data_list[0].get('nick_name')
        age = data_list[0].get('age')
        gender = data_list[0].get('gender')
        id_card = data_list[0].get('id_card')
        phone = data_list[0].get('phone')
        area = data_list[0].get('area')
        signature = data_list[0].get('signature')
        avatar = data_list[0].get('avatar')
        type = data_list[0].get('type')
        career = data_list[0].get('career')
        lowest_wage = data_list[0].get('lowest_wage')
        highest_wage= data_list[0].get('highest_wage')
        profile= data_list[0].get('profile')
        # user = User.objects.get(id=id)
        request.user_obj.name = name
        request.user_obj.nick_name = nick_name
        request.user_obj.age= age
        request.user_obj.gender =gender
        request.user_obj.id_card = id_card
        request.user_obj.phone= phone
        request.user_obj.area=area
        request.user_obj.signature = signature
        request.user_obj.avatar=avatar
        request.user_obj.type = type
        request.user_obj.lowest_wage = lowest_wage
        request.user_obj.career = career
        request.user_obj.highest_wage = highest_wage
        request.user_obj.profile = profile
        request.user_obj.save()

        return JsonResponse({'success': True})
    else:
        return JsonResponse({'success': False})

@already_login
@csrf_exempt
def user_info(request):
    if request.method == "POST":
        data_list=[]
        object=request.user_obj
        print(object)
        data={
                'id':object.id,
                'age':object.age,
                'id_card':object.id_card,
                'phone':object.phone,
                'name':object.name,
                'gender':object.gender,
                'signature':object.signature,
                'nick_name':object.nick_name,
                'area':object.area,
                'avatar':object.avatar,
                'type': object.type,
                'career': object.career,
                'lowest_wage': object.lowest_wage,
                'highest_wage': object.highest_wage,
                'profile': object.profile,
            }
        data_list.append(data)
        return JsonResponse({'success': True, 'data': data_list})
    else:
        return JsonResponse({'success': False})

#得到省列表
def get_provinces(request):
    if request.method == "POST":
        url = 'https://restapi.amap.com/v3/config/district'
        params = {
            'key': 'af5035a414311145ba566b1d4d9cbab0',
            'keywords': '中国',
            'subdistrict': 1,
            'extensions': 'base'
        }

        response = requests.get(url, params=params)
        data = response.json()

        if data['status'] == '1' and data['count'] == '1':
            provinces = [province['name'] for province in data['districts'][0]['districts']]
            return JsonResponse({'success': True, 'provinces': provinces})
        else:
            return JsonResponse({'success': False})
    return JsonResponse({'success': False})
#得到相应市列表
def get_cities(request):
    if request.method == "POST":
        province = request.POST.get('province')
        api_key = "af5035a414311145ba566b1d4d9cbab0"
        url = f"https://restapi.amap.com/v3/config/district?key={api_key}&keywords={province}&subdistrict=1&extensions=all&level=city"
        response = requests.get(url)
        data = response.json()
        cities = []
        if data["status"] == "1":
            districts = data.get("districts", [])
            for district in districts:
                citie = district.get("districts", [])
                for city in citie:
                    city_name = city.get("name", "")
                    cities.append(city_name)
            return JsonResponse({'success': True, 'cities': cities})
        else:
            return JsonResponse({'success': False})
    return JsonResponse({'success': False})
#得到相应区列表
def get_districts(request):
    if request.method == "POST":
        city = request.POST.get('city')
        api_key = "af5035a414311145ba566b1d4d9cbab0"
        url = f"https://restapi.amap.com/v3/config/district?key={api_key}&keywords={city}&subdistrict=1&extensions=all"
        response = requests.get(url)
        data = response.json()
        districts = []
        if data["status"] == "1":
            districs = data.get("districts", [])
            for district in districs:
                if district["name"] == city:
                    areas = district.get("districts", [])
                    for area in areas:
                        area_name = area.get("name", "")
                        districts.append(area_name)
            return JsonResponse({'success': True, 'districts': districts})
        else:
            return JsonResponse({'success': False})
    return JsonResponse({'success': False})